/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Email        :   1047185209@QQ.com
 * FileName     :   CreateScriptAsset.cs
 * CreateData   :   2023/4/28 9:55:07
 * UnityVersion :   2020.3.33f1c2
 * Description  :   创建脚本资源模板
************************************************/

using Main;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;

public class CreateScriptAsset : Editor
{
    /// <summary>
    /// 获取选中目录
    /// </summary>
    /// <returns></returns>
    private static string GetSelectPathOrFallback()
    {
        string path = "Assets";
        foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
        {
            path = AssetDatabase.GetAssetPath(obj);
            if (!string.IsNullOrEmpty(path) && File.Exists(path))
            {
                path = Path.GetDirectoryName(path);
                break;
            }
        }
        return path + "/";
    }
    /// <summary>
    /// 判断脚本是否创建在热更目录内
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    private static bool IsCanCreateInHotfixPath(string path)
    {
        if (!path.Contains(PathConfig.HotfixPath))
        {
            bool limit = EditorUtility.DisplayDialog("警告", "所选目录不在热更(Hotfix)目录内!\n\n是否继续创建脚本。", "创建", "取消");
            return limit;
        }
        else
        {
            return true;
        }
    }

    [MenuItem("Assets/Create/创建Hotfix脚本/System", false, 80)]
    public static void CreateSystemLua()
    {
        string filePath = GetSelectPathOrFallback();
        if (IsCanCreateInHotfixPath(filePath))
        {
            ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
                ScriptableObject.CreateInstance<ScriptAssetFile>(),
                filePath + "NewSystemScript.lua", null,
                PathConfig.TemplatesPath + "System.txt");
        }
    }


    /// <summary>
    /// 脚本资源对象
    /// </summary>
    class ScriptAssetFile : EndNameEditAction
    {
        public override void Action(int instanceId, string pathName, string resourceFile)
        {
            UnityEngine.Object obj = CreateScriptAssetFromTemplate(pathName, resourceFile);
            ProjectWindowUtil.ShowCreatedAsset(obj);
        }
        internal static UnityEngine.Object CreateScriptAssetFromTemplate(string targetFile, string resourceFile)
        {
            // 文件信息
            FileInfo info = new FileInfo(targetFile);
            // 获取文件名不带后缀
            string fileName = info.Name.Substring(0, info.Name.Length - info.Extension.Length);
            // 获取文件名+文件描述
            string[] commandLine = fileName.Split(' ');
            // 如果有文件描述再更改描述
            if (commandLine[0].Length > 0 && commandLine.Length > 1)
            {
                fileName = commandLine[0];
                ScriptHeadWindow.Description = commandLine[1];
                targetFile = info.DirectoryName + "/" + fileName + info.Extension;
            }
            // 获取文件
            string content = File.ReadAllText(resourceFile);
            // 替换脚本名
            content = content.Replace("#SCRIPTNAME#", fileName);
            // 替换描述
            content = content.Replace("#DESCRIPTION#", ScriptHeadWindow.Description);
            // 替换内容
            content = content.Replace("#SCRIPTCONTENT#", "");

            // 写入文件
            File.WriteAllText(targetFile, content);
            // 导入文件
            AssetDatabase.ImportAsset(targetFile);
            // 刷新
            AssetDatabase.Refresh();
            // 返回脚本资源对象
            return AssetDatabase.LoadAssetAtPath(targetFile, typeof(UnityEngine.Object));
        }
    }
}
